package libra;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import iRule.RuleConfig;

/**
 * SpringBoot启动入口 
 * @author libra
 */
//标注该类为SpringBoot的主配置类
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})// 排除前端门户的数据库自动装配
//指定包扫描
@ComponentScan(value="libra")
// 扫描Servlet组件
@ServletComponentScan(value="libra")
/*
 * @EnableEurekaClient 与 @EnableDiscoveryClient
 * 在功能上是一致的：写在启动类的上，开启服务注册发现功能。
 * @EnableEurekaClient只适用于Eureka作为注册中心，@EnableDiscoveryClient 可以是其他注册中心。
 */
// 开启EurekaClient的注解，连接EurekaServer
@EnableEurekaClient
//@EnableDiscoveryClient
/*
 * 默认不需要该配置注解，当我们需要替换Ribbon的默认负载均衡算法【轮询】时，需要手动配置
 * name：被调用的微服务名称
 * configuration：指定负载均衡替换的配置类，从boot主启动类的@ComponentScan包外进行加载
 */
@RibbonClient(name="PROVIDER-PAYMENT",configuration = RuleConfig.class)
public class MainStart_Consumer_Ribbon_10300 {
	public static void main(String[] args) {
		System.out.println("MainStart.main()");
		ConfigurableApplicationContext cac = SpringApplication.run(MainStart_Consumer_Ribbon_10300.class, args);
	}
	
	@Bean
	@LoadBalanced
	public RestTemplate restTemplate() {
		RestTemplate restTemplate = new RestTemplate();
		List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
		list.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
		list.add(new ByteArrayHttpMessageConverter());
		list.add(new AllEncompassingFormHttpMessageConverter());
		list.add(new MappingJackson2HttpMessageConverter());
		restTemplate.setMessageConverters(list);
		return restTemplate;
	}
}
